package com.pda.xinma.presenter;

import android.os.Bundle;

import com.pda.xinma.bean.ArtificialListBean;
import com.pda.xinma.bean.EndFaceListBean;
import com.pda.xinma.bean.WorkSurfaceBean;
import com.pda.xinma.contract.ManMadeContract;
import com.pda.xinma.net.NetUtils;
import com.wangshen.base.app.AppActivityKey;
import com.wangshen.base.config.StringKey;
import com.wangshen.base.net.bean.BaseAppEntity;
import com.wangshen.base.net.bean.BaseRowsAppEntity;
import com.wangshen.base.ui.mvp.base.presenter.BasePresenter;

import java.util.List;

import io.reactivex.functions.Consumer;

public class ManMadePresenter extends BasePresenter<ManMadeContract .View>implements ManMadeContract.Presenter {
    @Override
    public void getCenterByUserId() {
        NetUtils.geApi().getCenterByUserId()
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseAppEntity<List<WorkSurfaceBean>>>() {
                    @Override
                    public void accept(BaseAppEntity<List<WorkSurfaceBean>> listBaseAppEntity) throws Exception {
                        getView().setFaceList(listBaseAppEntity.getData());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void selectScanArtificialDeliveryList(String warehouseId, String centerCode, String orderNo) {
        NetUtils.geApi().selectArtificialDeliveryList(warehouseId, centerCode,orderNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<EndFaceListBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<EndFaceListBean>> listBaseRowsAppEntity) throws Exception {
                       // getView().setList(listBaseRowsAppEntity.getRows());
                        if (listBaseRowsAppEntity.getRows().size() > 0){
                            EndFaceListBean endFaceListBean=listBaseRowsAppEntity.getRows().get(0);
                            endFaceListBean.setCenterCode(centerCode);
                            Bundle bundle=new Bundle();
                            bundle.putSerializable(StringKey.QQ,endFaceListBean);
                            getView().startIntent(AppActivityKey.ENDFACEDETAILACTIVITY,bundle);
                        }else {
                            getView().showMessage("未查询到数据！");
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void selectSearchArtificialDeliveryList(String warehouseId, String centerCode, String orderNo) {
        NetUtils.geApi().selectArtificialDeliveryList(warehouseId, centerCode,orderNo)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<EndFaceListBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<EndFaceListBean>> listBaseRowsAppEntity) throws Exception {
                        if (listBaseRowsAppEntity.getRows().size() == 0){
                            getView().showMessage("未查询到数据！");
                        }else {
                            getView().setList(listBaseRowsAppEntity.getRows());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void selectArtificialList(String warehouseId, String centerCode, String orderNo, String startTime, String endTime) {
        NetUtils.geApi().selectArtificialList(warehouseId, centerCode, orderNo, startTime, endTime)
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<ArtificialListBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<ArtificialListBean>> listBaseRowsAppEntity) throws Exception {
                       /* if (listBaseRowsAppEntity.getRows().size() == 0){
                            getView().showMessage("未查询到数据！");
                        }else {*/
                            getView().setNewList(listBaseRowsAppEntity.getRows());
                        //}
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }

    @Override
    public void selectArtificialDeliveryList(String warehouseId, String centerCode) {
        NetUtils.geApi().selectArtificialDeliveryList(warehouseId, centerCode,"")
                .compose(this.handleEverythingResult())
                .subscribe(new Consumer<BaseRowsAppEntity<List<EndFaceListBean>>>() {
                    @Override
                    public void accept(BaseRowsAppEntity<List<EndFaceListBean>> listBaseRowsAppEntity) throws Exception {
                        getView().setList(listBaseRowsAppEntity.getRows());
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        getView().showMessage(throwable.getMessage());
                    }
                });
    }
}
